package top.yzhelp.enjoyinformation.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import top.yzhelp.enjoyinformation.controller.res.Result;
import top.yzhelp.enjoyinformation.model.Apply;
import top.yzhelp.enjoyinformation.model.Book;
import top.yzhelp.enjoyinformation.service.ApplyService;
import top.yzhelp.enjoyinformation.service.BookService;
import top.yzhelp.enjoyinformation.shiro.ShiroRealm;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 码之泪殇 GitHub: https://github.com/gongsir0630
 * @date 2021/3/26 12:46
 * 你的指尖,拥有改变世界的力量
 * @description 二手书接口
 */
@RestController
@RequestMapping("book")
@Slf4j
@RequiresAuthentication
public class BookController {

    @Resource
    private BookService bookService;
    @Resource
    private ApplyService applyService;

    /**
     * 从认证信息中获取用户Id: userId == openId
     * @return userId
     */
    private String getUserId() {
        return ShiroRealm.getUserId();
    }

    /**
     * 申请查看联系方式
     * @param id 二手书 id
     * @return status
     */
    @GetMapping("/concat/{id}")
    public ResponseEntity<Result<String>> applyConcat(@PathVariable("id") int id) {
        Apply apply = new Apply();
        apply.setFromUser(getUserId());
        apply.setBookId(id);
        apply.setStatus("no");
        applyService.save(apply);
        return new ResponseEntity<>(Result.success("已向用户发送申请"), HttpStatus.OK);
    }

    /**
     * 二手书列表
     * @description 支持关键字和分页查询
     * @param page 当前页,默认 1
     * @param size 每页数量,默认 10
     * @param keyword 关键字
     * @return 所有二手书信息
     */
    @GetMapping("list")
    public ResponseEntity<Result<IPage<Book>>> getAllBooks(@RequestParam(required = false,defaultValue = "1") int page,
                                                          @RequestParam(required = false,defaultValue = "10") int size,
                                                          @RequestParam(required = false,defaultValue = "") String keyword) {
        IPage<Book> bookPage = new Page<>(page,size);
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            // 关键字查询
            wrapper.like("bname",keyword).or().like("describle",keyword);
        }
        // 倒序排序
        wrapper.orderByDesc("id");
        IPage<Book> books = bookService.page(bookPage, wrapper);
        return new ResponseEntity<>(Result.success(books), HttpStatus.OK);
    }

    /**
     * 单个二手书信息
     * @param id 二手书 id
     * @return 二手书信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Result<Book>> getBookById(@PathVariable("id") int id) {
        return new ResponseEntity<>(Result.success(bookService.getById(id)), HttpStatus.OK);
    }

    /**
     * 修改二手书信息
     * @param id id
     * @param book 新的二手书信息
     * @return 更新后的二手书信息
     */
    @PostMapping("/{id}")
    public ResponseEntity<Result<Book>> updateBook(@PathVariable("id") int id,
                                                   Book book) {
        book.setId(id);
        book.setPublishTime(new Date());
        bookService.updateById(book);
        book = bookService.getById(id);
        return new ResponseEntity<>(Result.success(book), HttpStatus.OK);
    }

    /**
     * 删除二手书
     * @param id 二手书 id
     * @return 二手书信息
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Result<Book>> delBookById(@PathVariable("id") int id) {
        Book book = bookService.getById(id);
        bookService.removeById(id);
        return new ResponseEntity<>(Result.success(book), HttpStatus.OK);
    }

    /**
     * 新增二手书
     * @param book 二手书信息, 用户 id 不需要
     * @return status
     */
    @PutMapping("")
    public ResponseEntity<Result<String>> saveBook(Book book) {
        book.setUserId(getUserId());
        book.setPublishTime(new Date());
        bookService.save(book);
        return new ResponseEntity<>(Result.success("save book success"), HttpStatus.OK);
    }
}
